package com.qf.service.impl;

import com.qf.entity.SysMenu;
import com.qf.entity.SysUserInfo;
import com.qf.events.MenuChangeEvent;
import com.qf.mapper.SysMenuDao;
import com.qf.mapper.SysMenuMapper;
import com.qf.pojo.BaseUser;
import com.qf.service.MenuService;
import com.qf.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Author ztf
 * @Date 11:42 2022/1/10
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class MenuServiceImpl implements MenuService {
    private SysMenuDao sysMenuDao;
    private SysMenuMapper sysMenuMapper;
    private ApplicationContext context;

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Autowired
    public void setSysMenuMapper(SysMenuMapper sysMenuMapper) {
        this.sysMenuMapper = sysMenuMapper;
    }

    @Autowired
    public void setSysMenuDao(SysMenuDao sysMenuDao) {
        this.sysMenuDao = sysMenuDao;
    }

    @Override
    public void updateMenu(SysMenu menu) {
        BaseUser user = SecurityUtils.getUser();
        menu.setUpdateBy(user.getUsername());
        menu.setUpdateTime(new Date());
        sysMenuDao.updateByPrimaryKey(menu);
        context.publishEvent(new MenuChangeEvent());
    }

    @Override
    public void updateMenuEnable(String enable, Integer menuId) {
        sysMenuMapper.updateMenuEnable(enable, menuId);
        context.publishEvent(new MenuChangeEvent());
    }
}
